package com.yanqu.road.dao.impl.match;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.match.MatchPatronsData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/2/18 10:02
 */
public class MatchPatronsDataDaoImpl extends TempDataObjectDao<MatchPatronsData> {

    public static String addSql = "INSERT INTO t_u_variety_patrons_data(userId, patronsId, matchBattleTimes," +
            "recoveryTime, battleTimeId) values(?,?,?,?,?)";
    @Override
    protected boolean addData(MatchPatronsData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMatchBattleTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRecoveryTime()));
        para.put(i, new DbParameter(Types.INTEGER, data.getBattleTimeId()));

        if (getDb().executeNoneQuery(addSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_u_variety_patrons_data set matchBattleTimes = ?," +
            "recoveryTime = ?, battleTimeId = ? WHERE userId = ? AND patronsId = ?";

    @Override
    protected boolean updateData(MatchPatronsData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getMatchBattleTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRecoveryTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBattleTimeId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i, new DbParameter(Types.INTEGER, data.getPatronsId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected MatchPatronsData parseData(ResultSet rs) throws SQLException {
        MatchPatronsData matchPatronsData = new MatchPatronsData();
        matchPatronsData.setUserId(rs.getLong("userId"));
        matchPatronsData.setPatronsId(rs.getInt("patronsId"));
        matchPatronsData.setMatchBattleTimes(rs.getInt("matchBattleTimes"));
        matchPatronsData.setRecoveryTime(rs.getLong("recoveryTime"));
        matchPatronsData.setBattleTimeId(rs.getInt("battleTimeId"));
        return matchPatronsData;
    }

    public static String selectSql = "SELECT * FROM t_u_variety_patrons_data where userId = ?";
    public Map<Integer, MatchPatronsData> getMatchPatronsDataMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, MatchPatronsData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                MatchPatronsData matchPatronsData = parse(rs);
                map.put(matchPatronsData.getPatronsId(), matchPatronsData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
